package com.andy.beef.ui.main.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.RecyclerView
import com.andy.beef.R
import com.andy.beef.databinding.MainActivityChooseFenceBinding
import com.andy.beef.ui.main.adapter.FenceChooseAdapter
import com.andy.beef.ui.main.bean.FenceBean
import com.andy.beef.ui.main.viewmodel.ChooseFenceViewModel
import com.andy.module.base.BaseVbPageListActivity
import com.andy.module.ext.addToolBar
import com.andy.module.widget.CustomToolBar
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.andy.ui.ext.clickNoRepeat

/**
 * @creator:LuoFan
 * @date:2022/6/6
 * @description:选择围栏
 */
class ChooseFenceActivity :
    BaseVbPageListActivity<ChooseFenceViewModel, MainActivityChooseFenceBinding, FenceBean>() {

    private lateinit var mToolbar: CustomToolBar

    private var mAdapter = FenceChooseAdapter()

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mToolbar = mViewBind.root.addToolBar(title = "选择围栏") {
            finish()
        }
        mToolbar.setRightBtnText(getString(R.string.main_confirm))
        mToolbar.getRightBtn().clickNoRepeat {
            if (mAdapter.chooseFenBean != null) {
                onSelect()
            } else {
                ToastUtils.showShort("请选择")
            }
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            val bean = mAdapter.getItem(position)
            mAdapter.setSelectSource(bean)
        }
    }

    private fun onSelect() {
        val intent = Intent()
        intent.putExtra(RESULT_DATA, mAdapter.chooseFenBean)
        setResult(Activity.RESULT_OK, intent)
        finish()
    }

    override fun getAdapter(): BaseQuickAdapter<FenceBean, BaseViewHolder> {
        return mAdapter
    }

    override fun getRecyclerView(): RecyclerView {
        return mViewBind.recyclerView
    }

    override fun getRefreshLayout(): SmartRefreshLayout {
        return mViewBind.refreshLayout
    }

    companion object {

        const val CODE = 100
        const val RESULT_DATA = "resultData"

        fun startMeForResult(context: Activity, code: Int) {
            val intent = Intent(context, ChooseFenceActivity::class.java)
            context.startActivityForResult(intent, code)
        }
    }
}